package com.sinaapp.gavinzhang.webserver;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import java.io.*;
import java.util.Locale;

/**
 * Created by gavin on 15-8-13.
 */
public class Response implements ServletResponse{

    //public static final String WEB_ROOT = System.getProperty("user.dir")+ File.separator+"webroot";
    public static final String WEB_ROOT = "/var/www/html/old";

    private OutputStream outputStream;

    private Request request;

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void setRequest(Request request){
        this.request = request;
    }

    public void sendStaticResource()throws IOException{
        InputStream fis = null;
        try {
            File file = new File(WEB_ROOT+request.getUri());
            System.out.println(file.toString());
            if (file.exists())
            {
                fis = new FileInputStream(file);
                PrintStream out = new PrintStream(outputStream);
                out.print(new String(readBytes(fis)));
            }else {
                test();
            }

        }catch (Exception e){
            e.printStackTrace();
            test();
        }
    }

    public int test()throws IOException{
        String header = "HTTP/1.1 200 OK\r\n"+"Content-Type=text/html\r\n"+"Content-Length:"+request.header.length()+"\r\n"+"\r\n"+request.header;
        PrintStream out = new PrintStream(outputStream);
        out.print(header);


        //outputStream.write(header.getBytes(),0,header.getBytes().length);
        return 1;
    }

    public static byte[] readBytes(InputStream in) throws IOException {
        BufferedInputStream bufin = new BufferedInputStream(in);
        int buffSize = 1024;
        ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);

        // System.out.println("Available bytes:" + in.available());

        byte[] temp = new byte[buffSize];
        int size = -1;
        while ((size = bufin.read(temp)) != -1) {
            out.write(temp, 0, size);
            if (size!=1024)break;
        }
        bufin.close();

        byte[] content = out.toByteArray();
        return content;
    }


    /** implements*/
    public String getCharacterEncoding() {
        return null;
    }

    public String getContentType() {
        return null;
    }

    public ServletOutputStream getOutputStream() throws IOException {
        return null;
    }

    public PrintWriter getWriter() throws IOException {
        PrintWriter writer = new PrintWriter(outputStream,true);
        return writer;
    }

    public void setCharacterEncoding(String s) {

    }

    public void setContentLength(int i) {

    }

    public void setContentLengthLong(long l) {

    }

    public void setContentType(String s) {

    }

    public void setBufferSize(int i) {

    }

    public int getBufferSize() {
        return 0;
    }

    public void flushBuffer() throws IOException {

    }

    public void resetBuffer() {

    }

    public boolean isCommitted() {
        return false;
    }

    public void reset() {

    }

    public void setLocale(Locale locale) {

    }

    public Locale getLocale() {
        return null;
    }
}
